#include "scene_cell.h"
#include "scene_instance.h"
#include "../share/retcodes.h"

CSceneCell::CSceneCell( )
{
	clear( );
}

CSceneCell::~CSceneCell( )
{
	clear( );
}

void CSceneCell::clear( )
{
	cell_index_ = 0;
}

int32_t CSceneCell::cell_index( )
{
	return cell_index_;
}

::common::CRect& CSceneCell::cell_rect( )
{
	return cell_rect_;
}

int32_t CSceneCell::Init( CSceneInstance& scene, int32_t cell_index, int32_t vision_radius_width, int32_t vision_radius_heigth )
{
	cell_index_ = cell_index;
	scene.GetRectInRadius( cell_index_, vision_radius_width, vision_radius_heigth, cell_rect_ );
	return common::SUCCEED;
}

CSceneCellMgr::CSceneCellMgr( )
{
	cell_size_ = 0;
	scene_cell_list_ = NULL;
}

CSceneCellMgr::~CSceneCellMgr( )
{
	clear( );
}

void CSceneCellMgr::clear( )
{
	if( scene_cell_list_ != NULL )
	{
		delete[ ] scene_cell_list_;
		scene_cell_list_  = NULL;
	}

	cell_size_ = 0;
}

CSceneCell* CSceneCellMgr::GetSceneCell( int32_t cell_index ) const
{
	if( cell_index < 0 || cell_index >= cell_size_ ) return NULL;
	return scene_cell_list_ + cell_index;
}

int CSceneCellMgr::Init( CSceneInstance& instance, int32_t cell_size, int32_t vision_width, int32_t vision_height )
{
	cell_size_ = cell_size;
	
	scene_cell_list_ = new CSceneCell[ cell_size ];

	for( int32_t i = 0; i < cell_size_; ++i )
	{
		scene_cell_list_[ i ].Init( instance, i, vision_width, vision_height );
	}

	return common::SUCCEED;
}

